import {createRouter, createWebHistory, type RouteRecordRaw} from 'vue-router'
import {storageHook} from "@/hooks/storagehook.ts";
import {StorageStrategy} from "@/util/storage.ts";
import {REFRESH_TOKEN_KEY, TOKEN_KEY} from "@/common/comConstant/commoncanstant.ts";
import {sendGlobalMessage} from "@/hooks/globalmessage.ts";
import {GlobalMessage, MessageType} from "@/event/eventglobalmessage.ts";

//静态白名单路由
const staticWhiteRoutes = <RouteRecordRaw[]>[
    {
        path: "/login",
        name: "login",
        component: () => import('@/views/login/login.vue')
    },
    {
        path: "/404",
        name: "404",
        component: () => import("@/views/system/common/404.vue")
    },
    {
        path: "/502",
        name: "502",
        component: () => import("@/views/system/common/502.vue")
    },


]
// 固定路由
const fixedRoutes = [
    {
        path: '/',
        redirect: '/layout',
    },
    {
        path: '/layout',
        name: 'layout',
        component: () => import('@/layout/layout.vue'),
        redirect: "/home",
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/home/home.vue'),
            },
            {
                path: "/menu",
                name: "menu",
                component: () => import('@/views/system/menu/menu.vue')
            },
            {
                path: "/role",
                name: "role",
                component: () => import("@/views/system/role/role.vue")
            },
            {
                path: "/user",
                name: "user",
                component: () => import("@/views/system/user/user.vue")
            },
            {
                path: "/notice",
                name: "notice",
                component: () => import("@/views/system/notice/notice.vue")
            },
            {
                path: "/iconChange",
                name: "iconChange",
                component: () => import("@/views/customComp/compIconChange.vue")
            },
            {
                path: "/menuItem",
                name: "menuItem",
                component: () => import("@/views/customComp/compMenuItem.vue")
            },
            {
                path: "/pageTable",
                name: "pageTable",
                component: () => import("@/views/customComp/compPageTable.vue")
            }

        ]
    }
]

//动态业务路由


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [...staticWhiteRoutes, ...fixedRoutes]
})
const VITE_UI_MODE = JSON.parse(import.meta.env.VITE_UI_MODE) || false
//路由守卫
router.beforeEach((to, from, next) => {
    //访问路径不存在
    if (!to.matched.length) {
        next({path: '/404'});
    } else if (VITE_UI_MODE) {
        next()
    } else {
        //路由白名单
        if (staticWhiteRoutes.map(item => item.path).includes(to.path)) {
            next();
        } else {
            //获取token
            const token = storageHook(StorageStrategy.COOKIE_STORAGE).storage.get(TOKEN_KEY)
            const refreshToken = storageHook(StorageStrategy.COOKIE_STORAGE).storage.get(REFRESH_TOKEN_KEY)
            if (token || refreshToken) {
                next();
            } else {
                sendGlobalMessage(new GlobalMessage().buildType(MessageType.ERROR).buildMessage("凭证失败,请重新登录"))
                setTimeout(() => {
                    router.push("/login").then(res => {
                    })
                }, 1500)

            }
        }
    }


})
export default router
